//
//  ViewController.m
//  视频悬浮框OC
//
//  Created by 解楚豪 on 2018/9/10.
//  Copyright © 2018年 xch. All rights reserved.
//

#import "ViewController.h"
#import "VideoPlayViewController.h"
#import "XCHPlay.h"
#import "XCHVideoView.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIView *videoView;

@property (nonatomic) NSInteger count;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(playNextAction)];
    [self.videoView addGestureRecognizer:tap];
}

-(void)playNextAction{
    [self deleteVideo:NO];
    [self playVideo];
}

-(void)playVideo{

    VideoPlayViewController *playVC = [[VideoPlayViewController alloc]init];
    if (self.count == 1) {
        playVC.videoUrl = @"http://media.nenghealth.com/upload/20180402/20180402125430_365.mp4";
    }else if (self.count == 2){
        playVC.videoUrl = @"http://media.nenghealth.com/upload/20180427/20180427185000_194.mp4";
    }
    __weak typeof(self) weakSelf = self;
    playVC.backBlock = ^{
        
        XCHVideoView *videoView = [[XCHVideoView alloc]initWithFrame:self.videoView.bounds];
        if (weakSelf.count == 1) {
            videoView.url = @"http://media.nenghealth.com/upload/20180402/20180402125430_365.mp4";
        }else if (weakSelf.count == 2){
            videoView.url = @"http://media.nenghealth.com/upload/20180427/20180427185000_194.mp4";
        }
        [weakSelf.videoView addSubview:videoView];
    };
    [self presentViewController:playVC animated:YES completion:nil];

}

#pragma mark ---------------方法-----------------
-(void) deleteVideo:(BOOL)isYes{
    if (isYes) {
        [[XCHPlay shareInstance] deleteVideoLayer];
    }
    for (UIView *view in self.videoView.subviews) {
        [view removeFromSuperview];
    }
}

#pragma mark ---------------IBACTION--------------------
- (IBAction)playAction:(id)sender {
    self.count = 1;
    [self deleteVideo:YES];
    [self playVideo];
}

- (IBAction)twoPlayAction:(id)sender {
    self.count = 2;
    [self deleteVideo:YES];
    [self playVideo];
}

-(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    CGPoint prePoint = [touch previousLocationInView:self.view];
//    CGFloat offsetX = prePoint.x - startpoint.x;
    
    if ([touches anyObject].view == self.videoView) {
        self.videoView.center = prePoint;
    }
    
    
}

-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
}

@end
